package com.cbank.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cbank.constants.Result;
import com.cbank.constants.ResultCode;
import com.cbank.domain.ModuleShow;
import com.cbank.domain.ModuleShowDetail;
import com.cbank.dto.ModuleShowDetailDto;
import com.cbank.service.ModuleService;
import com.cbank.service.ModuleShowDetailService;
import com.cbank.service.ModuleShowService;
import com.cbank.utils.JSONUtil;
import com.cbank.utils.StringUtil;
import com.cbank.vo.ModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/module")
public class ModuleController {
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private ModuleShowService moduleShowService;
    @Autowired
    private ModuleShowDetailService moduleShowDetailService;

    /**
     * 模块轮播图和模块的数据添加
     * @param moduleId 一级模块的ID
     * @param imageText 广告位的信息
     * @param moduleShowText 模块的信息
     */
    @RequestMapping(value = "/add/moduleshow",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveModule(@RequestParam(value = "moduleId",required = true)Integer moduleId, @RequestParam(value = "imageText",required = false,defaultValue = "")String imageText,
                                                @RequestParam(value = "moduleShowText",required = false,defaultValue = "")String moduleShowText){
        //添加广告位图片
        JSONArray imageArray=JSONUtil.parseArray(imageText);
        for (int i=0;i<imageArray.size();i++){
            JSONObject image=JSONUtil.parseObject(imageArray.get(i).toString());
            Integer moduleShowId=image.getInteger("moduleShowId");
            String url=image.getString("url");
            //String title=image.getString("title");
            ModuleShow moduleShow=this.moduleShowService.getById(moduleShowId);
            if (moduleShow == null){
                moduleShow=new ModuleShow();
                moduleShow.setModuleId(moduleId);
                moduleShow.setId(moduleShowId);
                moduleShow.setPictureUrl(url);
                //moduleShow.setTitle(title);
                moduleShow.setType(ModuleShow.TYPE_GG);//新增为广告位
                this.moduleShowService.save(moduleShow);
            }else {
                moduleShow.setPictureUrl(url);
                //moduleShow.setTitle(title);
                this.moduleShowService.save(moduleShow);
            }
        }

        //添加模块
        JSONArray moduleShowArray=JSONUtil.parseArray(moduleShowText);
        for (int i=0;i<moduleShowArray.size();i++){
            JSONObject moduleShowDB=JSONUtil.parseObject(moduleShowArray.get(i).toString());
            Integer moduleShowId=moduleShowDB.getInteger("moduleShowId");
            String title=moduleShowDB.getString("title");
            String enTitle=moduleShowDB.getString("enTitle");
            String url=moduleShowDB.getString("url");
            ModuleShow moduleShow=this.moduleShowService.getById(moduleShowId);
            if (moduleShow == null){
                moduleShow=new ModuleShow();
                moduleShow.setModuleId(moduleId);
                moduleShow.setPictureUrl(url);
                moduleShow.setTitle(title);
                moduleShow.setEnTitle(enTitle);
                moduleShow.setType(ModuleShow.TYPE_CON);//新增为模块
                this.moduleShowService.save(moduleShow);
            }else {
                moduleShow.setPictureUrl(url);
                moduleShow.setTitle(title);
                moduleShow.setEnTitle(enTitle);
                this.moduleShowService.save(moduleShow);
            }
        }

        return new  Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(), StringUtil.EMPTY_STRING);
    }

    /**
     * 模块的轮播图和文章/链接的数据添加
     * @param imageText 轮播图数据
     * @param moduleShowDetailDto 接收类
     * @param contentText 文章/链接数据
     */
    @RequestMapping(value = "/add/moduleshowdetail",method = RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public Result<String> saveLModuleShowByModuleShowId(@RequestParam(value = "imageText",required = false,defaultValue = "")String imageText,ModuleShowDetailDto moduleShowDetailDto,
                                                    @RequestParam(value = "contentText",required = false,defaultValue = "")String contentText){
//        JSONArray imageArray=JSONUtil.parseArray(imageText);
//        for (int i=0;i<imageArray.size();i++){
//            JSONObject image=JSONUtil.parseObject(imageArray.get(i).toString());
//            Integer moduleShowId=image.getInteger("moduleShowId");
//            String url=image.getString("url");
//            String title=image.getString("title");
//            ModuleShow moduleShow=this.moduleShowService.getById(moduleShowId);
//            if (moduleShow==null){
//                moduleShow=new ModuleShow();
//                moduleShow.setTitle(title);
//                moduleShow.setPictureUrl(url);
//                moduleShow.setType(ModuleShow.TYPE_CON);
//                moduleShow.setContentType(ModuleShow.CONTENTTYPE_GG);
//            }
//        }

        //轮播图
        ModuleShow moduleShow=this.moduleShowService.getById(moduleShowDetailDto.getModuleShowId());
        if (moduleShow!=null){
            moduleShow.setBannerUrl(imageText);
            this.moduleShowService.save(moduleShow);
        }

        //文章或链接
        JSONArray contentArray=JSONUtil.parseArray(contentText);
        for (int i=0;i<contentArray.size();i++){
            JSONObject content=JSONUtil.parseObject(contentArray.get(i).toString());
            Integer moduleShowDetailId=content.getInteger("moduleShowDetailId");
            String title=content.getString("title");
            String enTitle=content.getString("enTitle");
            String url=content.getString("url");
            String cnLabel=content.getString("cnLabel");
            String enlabel=content.getString("enLabel");
            ModuleShowDetail moduleShowDetail=this.moduleShowDetailService.getById(moduleShowDetailId);
            if (moduleShowDetail==null){
                moduleShowDetail=new ModuleShowDetail();
                if (moduleShowDetailDto.getType()==ModuleShowDetail.CONTENTTYPE_WZ){
                    moduleShowDetail.setCnTitle(title);
                    moduleShowDetail.setUrl(url);
                    moduleShowDetail.setEnTitle(enTitle);
                    moduleShowDetail.setEnLabel(enlabel);
                    moduleShowDetail.setCnLabel(cnLabel);
                    moduleShowDetail.setType(moduleShowDetailDto.getType());
                    moduleShowDetail.setModuleShowId(moduleShowDetailDto.getModuleShowId());
                    this.moduleShowDetailService.save(moduleShowDetail);
                }else {
                    String subTitle=content.getString("subTitle");
                    String linkUrl=content.getString("linkUrl");
                    moduleShowDetail.setCnTitle(title);
                    moduleShowDetail.setType(moduleShowDetailDto.getType());
                    moduleShowDetail.setUrl(url);
                    moduleShowDetail.setCnSubTitle(subTitle);
                    moduleShowDetail.setLinkUrl(linkUrl);
                    moduleShowDetail.setModuleShowId(moduleShowDetailDto.getModuleShowId());
                    this.moduleShowDetailService.save(moduleShowDetail);
                }
            }else {
                if (moduleShowDetailDto.getType()==ModuleShowDetail.CONTENTTYPE_WZ){
                    moduleShowDetail.setCnTitle(title);
                    moduleShowDetail.setEnTitle(enTitle);
                    moduleShowDetail.setUrl(url);
                    moduleShowDetail.setEnLabel(enlabel);
                    moduleShowDetail.setCnLabel(cnLabel);
                    this.moduleShowDetailService.save(moduleShowDetail);
                }else {
                    String subTitle=content.getString("subTitle");
                    String linkUrl=content.getString("linkUrl");
                    moduleShowDetail.setCnTitle(title);
                    moduleShowDetail.setUrl(url);
                    moduleShowDetail.setCnSubTitle(subTitle);
                    moduleShowDetail.setLinkUrl(linkUrl);
                    this.moduleShowDetailService.save(moduleShowDetail);
                }
            }
        }
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }

    /**
     * 根据moduleShowDetail的id修改对应的数据信息(只修改文章,链接在'模块的轮播图和文章/链接的数据添加'接口进行修改)
     * @param moduleShowDetailDto 数据接收类
     * @param moduleShowDetailId moduleShowDetail的ID
     */
    @RequestMapping(value = "/save/moduleshowdetail",method = RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public Result<String> saveModuleShowDetailByModuleShowDetailId(ModuleShowDetailDto moduleShowDetailDto,@RequestParam(value = "moduleShowDetailId",required = true)Integer moduleShowDetailId){

        ModuleShowDetail moduleShowDetail=this.moduleShowDetailService.getById(moduleShowDetailId);
        moduleShowDetail.setBannerUrl(moduleShowDetailDto.getBannerUrl());
        moduleShowDetail.setCnTitle(moduleShowDetailDto.getCnTitle());
        moduleShowDetail.setEnTitle(moduleShowDetailDto.getEnTitle());
        moduleShowDetail.setCnSubTitle(moduleShowDetailDto.getCnSubTitle());
        moduleShowDetail.setEnSubTitle(moduleShowDetailDto.getEnSubTitle());
        moduleShowDetail.setCnArticle(moduleShowDetailDto.getCnArticle());
        moduleShowDetail.setEnArticle(moduleShowDetailDto.getEnArticle());
        moduleShowDetail.setArticleTime(moduleShowDetailDto.getArticleTime());
        moduleShowDetail.setEnArticleTime(moduleShowDetailDto.getEnArticleTime());
        this.moduleShowDetailService.save(moduleShowDetail);
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }

    /**
     * 根据moduleId获取模块下的轮播图和moduleShow的信息
     * @param moduleId
     * @return
     */
    @RequestMapping(value = "/show/moduleshow",method = RequestMethod.GET)
    @ResponseBody
    public Result<ModuleVo> moduleVo(@RequestParam(value = "moduleId",required = true)Integer moduleId){
        //获取轮播图数据
        List<ModuleShow> imageList=this.moduleShowService.getListByModuleIdAndType(moduleId,ModuleShow.TYPE_GG);
        //获取模块信息
        List<ModuleShow> moduleShowList=this.moduleShowService.getListByModuleIdAndType(moduleId,ModuleShow.TYPE_CON);

        ModuleVo moduleVo=new ModuleVo();
        moduleVo.setImageList(imageList);
        moduleVo.setModuleShowList(moduleShowList);
        return new Result<ModuleVo>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleVo);
    }

    /**
     * 根据moduleId获取模块下的moduleshow的信息
     * @param moduleId
     * @return
     */
    @RequestMapping(value = "/show/moduleshow/content",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<ModuleShow>> moduleShowContent(@RequestParam(value = "moduleId",required = true)Integer moduleId){
        List<ModuleShow> moduleShowList=this.moduleShowService.getListByModuleIdAndType(moduleId,ModuleShow.TYPE_CON);
        return new Result<List<ModuleShow>>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleShowList);
    }


    /**
     * 获取模块下的文章/链接信息
     * @param moduleShowId
     * @param type
     * @return
     */
    @RequestMapping(value = "/show/moduleshow/module",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<ModuleShowDetail>> moduleShowDetail(@RequestParam(value = "moduleShowId",required = true)Integer moduleShowId,@RequestParam(value = "type",required = false,defaultValue = "1")Integer type){
        //获取文章/链接的信息
        List<ModuleShowDetail> moduleShowDetailList=this.moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId,type);
        return new Result<List<ModuleShowDetail>>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleShowDetailList);
    }

    /**
     * 获取模块下的轮播图
     * @param moduleShowId
     * @return
     */
    @RequestMapping(value = "/show/moduleshow/banner",method = RequestMethod.GET)
    @ResponseBody
    public Result<String> moduleShowImage(@RequestParam(value = "moduleShowId",required = true)Integer moduleShowId){
        ModuleShow moduleShow=this.moduleShowService.getById(moduleShowId);
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleShow.getBannerUrl());
    }

    /**
     * 展示单个moduleShowDetail的详细信息
     * @param moduleShowDetailId
     * @return
     */
    @RequestMapping(value = "/show/moduleshowdetail",method = RequestMethod.GET)
    @ResponseBody
    public Result<ModuleShowDetail> moduleShowDetail(@RequestParam(value = "moduleShowDetailId",required = true)Integer moduleShowDetailId){
        ModuleShowDetail moduleShowDetail=this.moduleShowDetailService.getById(moduleShowDetailId);
        return new Result<ModuleShowDetail>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleShowDetail);
    }

    /**
     * 将模块添加到回顾区
     * @param moduleShowDetailId
     * @param moduleShowId
     * @return
     */
    @RequestMapping(value = "/save/moduleshow/status",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveStatus(@RequestParam(value = "moduleShowId",required = true)Integer moduleShowId,@RequestParam(value = "moduleShowDetailId",required = true)Integer moduleShowDetailId){
        this.moduleShowDetailService.updateModuleShowId(moduleShowId,moduleShowDetailId);
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }

    /**
     * 修改模块数据失效
     * @param moduleShowId
     * @return
     */
    @RequestMapping(value = "/delete/moduelShow",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> deleteModuleShow(@RequestParam(value = "moduleShowId",required = true)Integer moduleShowId){
        this.moduleShowService.updateStatus(ModuleShow.STATUS_INVALID,moduleShowId);
        return new Result<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }

    /**
     * 修改文章数据失效
     * @param moduleShowDetailId
     * @return
     */
    @RequestMapping(value = "/delete/moduleShowDetail",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> deleteModuleShowDetail(@RequestParam(value = "moduleShowDetailId",required = true)Integer moduleShowDetailId){
        this.moduleShowDetailService.updateStatus(ModuleShowDetail.STATUS_INVALID,moduleShowDetailId);
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }
}
